'use strict';

module.exports = app => {
  return class extends app.Controller {
    * index(ctx) {
        const query=ctx.request.query;
        const wechatId=ctx.request.header.wechatid;
        var pageParams='';
        if(query.pageParams){
            pageParams =JSON.parse(ctx.request.query.pageParams);
            if(!pageParams.order){
                pageParams.order=[['createdAt','DESC']];
            }
        }else {
            pageParams={
                pageNum:1,
                pageLimit:6,
                pageTotal:0,
                order:[['createdAt','DESC']]
            }
        }
        var offset=(pageParams.pageNum - 1) * pageParams.pageLimit;
        var items=yield ctx.model.Banner.findAndCount({where:{wechatId:wechatId},limit:pageParams.pageLimit,offset:offset,order:pageParams.order});
        pageParams.pageTotal=Math.ceil(items.count/pageParams.pageLimit);
        yield ctx.body={
            data:items.rows,
            pageParams:pageParams
        };
    }
  };
};
